//
//  KYFViewController.m
//  musicPlayer
//
//  Created by adminstrator on 13-8-9.
//  Copyright (c) 2013年 yufeng. All rights reserved.
//

#import "KYFAppDelegate.h"
#import "KYFViewController.h"
#import "tableKYFViewController.h"
#import "ViewUtils.h"

@interface KYFViewController ()
@property (assign,nonatomic)BOOL tag;
@end

@implementation KYFViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.title = @"播放器";
        self.tabBarItem.image = [UIImage imageNamed:@"金莎.png"];
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.tag=NO;
    self.navigationItem.title = @"播放器";
    self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];
//    self.navigationItem.leftBarButtonItem = [ViewUtils getUIBarButtonItem:[UIImage imageNamed:@"back.png"] target:self action:@selector(back)];
//    [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back.png"]];
//    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back.png"]];
    
    delege = [[UIApplication sharedApplication]delegate];
    
    NSString *musicPath = [[NSBundle mainBundle]pathForResource:self.music ofType:@"mp3"];
    NSURL *musicURL = [[[NSURL alloc]initFileURLWithPath:musicPath]autorelease];
    self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:musicURL error:nil];
    [self.player prepareToPlay];
    
    //一定要写
    self.player.delegate=self;
    
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(showTime) userInfo:nil repeats:YES];
    
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)playmusic {
    NSString *musicPath = [[NSBundle mainBundle]pathForResource:self.music ofType:@"mp3"];
    NSURL *musicURL = [[[NSURL alloc]initFileURLWithPath:musicPath]autorelease];
    self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:musicURL error:nil];
    self.player.delegate = self;
    [self.player prepareToPlay];
    [self.player play];
}

//当歌曲播完后自动播放下一首
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if (self.tag==YES) {
        self.myrow = arc4random()%[delege.array count];       //随机数
    }
    else {
        if (self.myrow==[delege.array count]-1) {
                    self.myrow = 0;
                }
                else {
                    self.myrow = self.myrow+1;;
                }
    }
    NSLog(@"%d",self.myrow);
    self.music = [delege.array objectAtIndex:self.myrow];
    [self playmusic];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)soundChange:(id)sender {
    self.player.volume = self.sound.value;
}


//当切换到歌曲列表时自动暂停
-(void)viewWillDisappear:(BOOL)animated {
    [self.player stop];
}


- (void)dealloc {
    [_sound release];
    [_progressSlider release];
    [_currentTime release];
    [_totalTime release];
    [_imageView release];
    [_musicName release];
    [super dealloc];
}

-(void)showTime{
    if((int)self.player.currentTime%60<10){
    self.currentTime.text = [NSString stringWithFormat:@"%d:0%d",(int)self.player.currentTime/60,(int)self.player.currentTime%60];
}
    else {
        self.currentTime.text = [NSString stringWithFormat:@"%d:%d",(int)self.player.currentTime/60,(int)self.player.currentTime%60];
    }
    self.progressSlider.value = self.player.currentTime/self.player.duration;
    
    if ((int)self.player.duration%60<10) {
        self.totalTime.text = [NSString stringWithFormat:@"%d:0%d",(int)self.player.duration/60,(int)self.player.duration%60];
    }
    else {
    self.totalTime.text = [NSString stringWithFormat:@"%d:%d",(int)self.player.duration/60,(int)self.player.duration%60];
    }
    self.musicName.text = self.music;
    
    if ([self.music isEqualToString:@"小虎队-爱"]) {
        self.imageView.image = [UIImage imageNamed:@"小虎队"];
    }
    else if([self.music isEqualToString:@"金莎 - 留下来"]||[self.music isEqualToString:@"金莎 - 星月神话"]) {
        self.imageView.image = [UIImage imageNamed:@"金莎"];
    }
    else if([self.music isEqualToString:@"林志颖-对望"]) {
        self.imageView.image = [UIImage imageNamed:@"林志颖"];
    }
    else if ([self.music isEqualToString:@"梁静茹 - 勇气"]) {
        self.imageView.image = [UIImage imageNamed:@"梁静茹"];
    }
    else if ([self.music isEqualToString:@"老狼-同桌的你"]) {
        self.imageView.image = [UIImage imageNamed:@"老狼"];
    }
}


-(NSString *)timeToString:(int)time//将时间以以下格式输出
{
    NSString *yourTime=[NSString stringWithFormat:@"%02d:%02d",time/60,time%60];
    return yourTime;
}

- (IBAction)start:(id)sender {
    UIButton *button = (UIButton *)sender;
    if (self.player.playing) {
    [self.player stop];
        [button setBackgroundImage:[UIImage imageNamed:@"alarm_pause_btn"] forState:UIControlStateNormal];
    }
    else {
        [self.player play];
        [button setBackgroundImage:[UIImage imageNamed:@"alarm_play_btn"] forState:UIControlStateNormal];
    }
}

//上一首
- (IBAction)up:(id)sender {
    if (self.player.playing) {
        [self.player stop];
    }
    if (self.myrow==0) {
        self.myrow = [delege.array count]-1;
    }
    else {
        self.myrow = self.myrow-1;
    }
    self.music = [delege.array objectAtIndex:self.myrow];
    [self playmusic];
}

//下一首
- (IBAction)down:(id)sender {
    if (self.player.playing) {
        [self.player stop];
    }
    if (self.myrow==[delege.array count]-1) {
        self.myrow = 0;
    }
    else {
        self.myrow = self.myrow+1;;
    }
    self.music = [delege.array objectAtIndex:self.myrow];
    [self playmusic];
}


- (IBAction)progressChange:(id)sender {
    self.player.currentTime = self.progressSlider.value*self.player.duration;
}


- (IBAction)return:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
